#include <glut/glut.h>
#include <stdlib.h>
#include <stdio.h>

#include "map.h"

// Initial square position and size
GLfloat x = 0.0f;
GLfloat y = 0.0f;
GLfloat rsize = 25;

// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;

// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;

int startx = 10;
int starty = 10;

void drawRectf(int x,int y,int rsize)
{
  glRectf(x, y, x + rsize, y - rsize);  
}

///////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
	// Clear the window with current clearing color
	glClear(GL_COLOR_BUFFER_BIT);
	
   	// Set current drawing color to red
	//		   R	 G	   B
	glColor3f(1.0f, 0.0f, 0.0f);
	
	// Draw a filled rectangle with current color
        //drawRectf(x,y,rsize);
        //	glRectf(x, y, x + rsize, y  +  rsize);
        //glRectf(10,10,15,15);

        drawBlock();
        drawShape();
	
    // Flush drawing commands and swap
    glutSwapBuffers();
}

void TimerFunction(int value)
{
    if(x > windowWidth-rsize || x < -windowWidth)
        xstep = -xstep;
	
    if(y > windowHeight || y < -windowHeight + rsize)
        ystep = -ystep;
	
    x += xstep;
    y += ystep;
	
    if(x > (windowWidth-rsize + xstep))
        x = windowWidth-rsize-1;
	else if(x < -(windowWidth + xstep))
		x = -windowWidth -1;
	
    if(y > (windowHeight + ystep))
        y = windowHeight-1; 
	else if(y < -(windowHeight - rsize + ystep))
		y = -windowHeight + rsize - 1;
    updateShape();
	
    glutPostRedisplay();
    #define TimeInterval 330
    glutTimerFunc(TimeInterval,TimerFunction, 1);
}


void SetupRC(void)
{
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    initMatrix();
}

void keyboard ( unsigned char key, int x, int y )  // Create Keyboard Function
{
  switch ( key ) {
  case 27:        // When Escape Is Pressed...
    exit ( 0 );   // Exit The Program
    break;        // Ready For Next Case
    //  case GLUT_KEY_LEFT:
  case 'd':
    MoveRight();
    break;
  case GLUT_KEY_UP:
  case 'w':
    ShapeChangeState();
    break;
    
  case GLUT_KEY_RIGHT:
  case 'a':
    MoveLeft();
    break;
  case GLUT_KEY_DOWN:
  case 's':
    MoveDown();
    break;
  default:        // Now Wrap It Up
    break;
  }
  /*
    GLUT_KEY_LEFT
    Left directional key. 
GLUT_KEY_UP
    Up directional key. 
GLUT_KEY_RIGHT
    Right directional key. 
GLUT_KEY_DOWN 
   */
}


///////////////////////////////////////////////////////////
// Called by GLUT library when the window has chanaged size
void ChangeSize(int w, int h)
{
    GLfloat aspectRatio;
	
    // Prevent a divide by zero
    if(h == 0)
        h = 1;
	
    // Set Viewport to window dimensions
    glViewport(0, 0, w, h);
	
    // Reset coordinate system
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	
    // Establish clipping volume (left, right, bottom, top, near, far)
    aspectRatio = (GLfloat)w / (GLfloat)h;
    if (w <= h) 
	{
        windowWidth = 100;
        windowHeight = 100 / aspectRatio;
        glOrtho (-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
	}
    else 
	{
        windowWidth = 100 * aspectRatio;
        windowHeight = 100;
        glOrtho (-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);
	}
	
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

///////////////////////////////////////////////////////////
// Main program entry point
int main(int argc, char* argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
  glutInitWindowSize(800,600);
  glutCreateWindow("Bounce");
  glutDisplayFunc(RenderScene);
  glutKeyboardFunc( keyboard );        
  glutReshapeFunc(ChangeSize);
  glutTimerFunc(TimeInterval, TimerFunction, 1);
	
  SetupRC();
	
  glutMainLoop();
	
  return 0;
}

/*
  movex movey

  blocksize*movex + blocksize*i
  blocksize*movey + blocksize*j

  int blocks[8][4][16] =  
{
// |
  {{  0,0,0,0,  1,1,1,1,  0,0,0,0,  0,0,0,0},
   {  0,1,0,0,  0,1,0,0,  0,1,0,0,  0,1,0,0},
   {  0,0,0,0,  1,1,1,1,  0,0,0,0,  0,0,0,0},
   {  0,1,0,0,  0,1,0,0,  0,1,0,0,  0,1,0,0}
  },
// j  
  {
  {0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},
  {1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
  {1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},
  {1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}
  },
  // l
  {
  {1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0},
  {0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0},
  {1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
  {1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0}
  },
  // o
  {
  {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
  {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
  {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
  {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}
  },
  // s
  {
  {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
  {1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
  {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
  {1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0}
  },
  // z
  {
  {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
  {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0}
  },
  // k
  {
  {1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
  {0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
  {1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0}
  }
};




  | j l | o s z k
*/


